package com.sky.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //还缺一步 还需要把菜品存在 setmeal_dish 中
        SetmealDish setmealDish = new SetmealDish();

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmealMapper.addSerMeal(setmeal);

        //这时候setmeal 的id就是空的 我需要等 setmeal 插进去 然后再获取他的id
        BigDecimal price = setmealDTO.getPrice();
        //先查到 他的setmealId
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(id);
            /*dish.setPrice(price);*/
            setmealMapper.addsetmealDish(dish);
        }
    }

    @Override
    public List<Dish> selectDishListByCaregorlyId(String categoryId) {
        List<Dish> list = setmealMapper.selectDishListByCaregorlyId(categoryId);
        return list;
    }

    @Override
    public PageResult selectSetMealList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> list = setmealMapper.selectSetMealList(setmealPageQueryDTO);

        //总记录数 还有集合
        return new PageResult(list.size(),list);
    }

    @Override
    public List<Category> selectCategoryById(int categoryId) {
       List<Category> list =  setmealMapper.selectCategoryById(categoryId);;
       return list;
    }

    @Override
    @Transactional
    public void deleteSetmeals(List<Long> ids) throws DeletionNotAllowedException {



        for (Long id : ids) {
            Integer status =  setmealMapper.selectStatusById(id , StatusConstant.ENABLE);
            log.info("状态为：{}",status);
            if (status > 0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

            Integer setmealDishId = setmealMapper.selectSetMealById(id);
            log.info("关联的：{}",setmealDishId);
            if (setmealDishId>0) {
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
            setmealMapper.deleteSetMeals(id);
            setmealMapper.deleteSetMealDishById(Long.valueOf(setmealDishId));
        }

    }

    @Override
    public SetmealVO selectSetmealById(Long setmealId) {
        //解决CategoryName
        SetmealVO result = setmealMapper.selectSetMealInfoById(setmealId);
        //解决关联关系
        result.setSetmealDishes(setmealMapper.selectSetMealDishById(setmealId));
        result.setCategoryName(setmealMapper.getCategoryName(setmealId));
        return result;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改setmealDish表
        //删除dish表
        setmealMapper.deleteSetMealDishById(setmealDTO.getId());
        //先删除原来的setmeal表
        setmealMapper.deleteSetMeals(setmealDTO.getId());


        //新增dish 表
        List<SetmealDish> list =  setmealDTO.getSetmealDishes();
        for (SetmealDish dish : list) {
            dish.setSetmealId(setmealDTO.getId());
            setmealMapper.addsetmealDish(dish);
        }
        //新增setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSerMeal(setmeal);
    }

    @Override
    @CacheEvict(value="SetMeal",allEntries = true)
    public void updateSetmealStatus(int status, String id) {
        setmealMapper.updateSetmealStatus(status ,id);
    }


}
